﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Growatt.CreateCCode
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using OfficeOpenXml;

    public class ExcelModel
    {
        public string StartIndex { get; set; }
        public int StartIndexint { get; set; }
        public string FileName { get; set; }
        public string FunctionName { get; set; }
        public string VariableName { get; set; }
        public string SheetName { get; set; }
        public string PointerName { get; set; }
        public string Name { get; set; }
        public string ColCount { get; set; }
    }

    public class ExcelModelTotal
    {
        public StringBuilder Sb { get; set; }
        public string FileName { get; set; }
        public string PonterName { get; set; }
    }

    public class ExcelReader
    {
        public List<ExcelModel> ReadExcelFileVer(string filePath)
        {
            var models = new List<ExcelModel>();
            // 设置 EPPlus 的许可证上下文（社区版）
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            using (var package = new ExcelPackage(new FileInfo(filePath)))
            {
                var worksheet = package.Workbook.Worksheets[0];
                int rowCount = worksheet.Dimension.Rows;
                int headerRow = 5; // 表头行是第5行

                for (int row = 6; row <= rowCount; row++) // 从第6行开始读取数据
                {
                    string Indx = worksheet.Cells[row, 1].Text; // A列
                    string fileName = worksheet.Cells[row, 7].Text; // G列
                    string functionName = worksheet.Cells[row, 8].Text; // H列

                    for (int col = 9; col <= worksheet.Dimension.Columns; col++) // 从I列开始遍历
                    {
                        if (worksheet.Cells[headerRow, col].Text.Contains("(1)"))
                        {
                            string variableName = worksheet.Cells[row, col].Text; // 获取表头单元格的地址
                            models.Add(
                                new ExcelModel
                                {
                                    StartIndex = Indx,
                                    FileName = fileName,
                                    FunctionName = functionName,
                                    VariableName = variableName,
                                    SheetName = worksheet.Name,
                                    Name = worksheet.Cells[headerRow, col].Text,
                                }
                            );
                            break; // 找到变量列后跳出循环
                        }
                    }
                }
            }

            return models;
        }

        public List<ExcelModel> ReadExcelFile(string filePath, int indexer = 10)
        {
            var models = new List<ExcelModel>();

            // 设置 EPPlus 的许可证上下文（社区版）
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

            using (var package = new ExcelPackage(new FileInfo(filePath)))
            {
                int headerRow = 5; // 表头行是第5行

                // 遍历所有工作表
                for (int sheetIndex = 0; sheetIndex < package.Workbook.Worksheets.Count; sheetIndex++)
                {
                    var worksheet = package.Workbook.Worksheets[sheetIndex];
                    int rowCount = worksheet.Dimension?.Rows ?? 0;

                    // 检查第五行的第G列是否有值
                    if (rowCount >= headerRow && !string.IsNullOrEmpty(worksheet.Cells[headerRow, 7].Text))
                    {
                        for (int row = 6; row <= rowCount; row++) // 从第6行开始读取数据
                        {
                            string indx = worksheet.Cells[row, 1].Text; // A列
                            string fileName = worksheet.Cells[row, 7].Text; // G列
                            string PonterName = worksheet.Cells[row, 8].Text; // H列
                            string functionName = worksheet.Cells[row, 9].Text; // H列

                            for (int col = indexer; col <= worksheet.Dimension.Columns; col++) // 从I列开始遍历
                            {
                                string variableName = worksheet.Cells[row, col].Text; // 获取表头单元格的地址
                                models.Add(
                                    new ExcelModel
                                    {
                                        StartIndex = indx,
                                        FileName = fileName,
                                        FunctionName = functionName,
                                        VariableName = variableName,
                                        SheetName = worksheet.Name,
                                        PointerName = PonterName,
                                        Name = worksheet.Cells[headerRow, col].Text,
                                        ColCount = worksheet.Dimension.Columns.ToString(),
                                    }
                                );
                                break; // 找到变量列后跳出循环
                            }
                        }
                    }
                }
            }

            return models;
        }

        public int ReadExcelFileColCount(string filePath, int indexer = 10)
        {
            var models = new List<ExcelModel>();
            // 设置 EPPlus 的许可证上下文（社区版）
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            using (var package = new ExcelPackage(new FileInfo(filePath)))
            {
                int headerRow = 5; // 表头行是第5行

                // 遍历所有工作表
                for (int sheetIndex = 0; sheetIndex < package.Workbook.Worksheets.Count; sheetIndex++)
                {
                    var worksheet = package.Workbook.Worksheets[sheetIndex];
                    int rowCount = worksheet.Dimension?.Rows ?? 0;

                    // 检查第五行的第G列是否有值
                    if (rowCount >= headerRow && !string.IsNullOrEmpty(worksheet.Cells[headerRow, 7].Text))
                    {
                        return worksheet.Dimension.Columns;
                    }
                }
            }
            return 0;
        }
    }
}
